package com.xxx.services;

import com.xxx.mapper.EmployeeMapper;
import com.xxx.mapper.UserMapper;
import com.xxx.pojo.Employee;
import com.xxx.pojo.Users;
import com.xxx.util.GetIdTime;
import com.xxx.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Scanner;

public class UserRegister {
    private static SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory();
    private Scanner sc = new Scanner(System.in);

    public void Register() {
        Long id = Long.valueOf(GetIdTime.getId() + "0");
        String username;
        String password;
        String sex;
        String phone;
        String createTime = GetIdTime.getDataTime();
        int status = 1;
        //获取对应的方法
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        System.out.println("=======用户注册======");
        System.out.println("请输入用户名：");
        while (true) {
            username = new Scanner(System.in).next();
            if (username.length() > 32)
                System.out.println("用户名过长，请重新输入：");
            else {
                Users users = null;
                try {
                    users = mapper.selectByUsername(username);
                } catch (Exception e) {

                }
                if (users == null)
                    sqlSession.close();
                break;
            }
        }


        System.out.println("请输入密码：");
        while (true) {
            password = sc.next();
            if (password.length() > 32)
                System.out.println("密码过长，请重新输入：");
            else
                break;
        }
        System.out.println("请确认密码：");
        while (true) {
            String password2 = sc.next();
            if (!password2.equals(password))
                System.out.println("两次密码不一致，请重新输入：");
            else
                break;
        }
        System.out.println("请输入性别：");
        while (true) {
            sex = sc.next();
            if (sex.equals("女") && sex.equals("男"))
                System.out.println("性别输入错误，请重新输入：");
            else
                break;
        }
        System.out.println("请输入手机号：");
        while (true) {
            Scanner s = new Scanner(System.in);
            try {
                phone = "" + s.nextLong();
                if (phone.length() != 11)
                    System.out.println("手机号错误，请重新输入：");
                else
                    break;
            } catch (Exception e) {

                System.out.println("手机号错误，请重新输入：");
            }

        }
        Users user = new Users(id,username,password,sex,phone,createTime,status);

        sqlSession = factory.openSession();
        mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.insertUser(user);
        if (i != 0) {
            System.out.println("注册成功");
            sqlSession.commit();
            sqlSession.close();
        } else
            System.out.println("注册失败");
    }


}
